package swu.edu.cn.javaee.controller;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.Comment;
import swu.edu.cn.javaee.entity.CommentInfo;
import swu.edu.cn.javaee.service.CommentService;
import java.util.List;
import static swu.edu.cn.javaee.component.Signal.*;

@Controller
//解决跨域问题：允许所有源域名访问
@CrossOrigin(origins = "*")

public class CommentController {

    @Resource
    private CommentService commentService;

    //新增评论
    @RequestMapping("/saveComment")
    @ResponseBody
    public String saveComment(String comment,String news_id,String user_id,String parent_id,String nickname){
        String result=commentService.saveComment(comment,news_id,user_id,parent_id,nickname);
        if (result!=null){
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_COMMENT;
        }

    }

    //用户查自己看所有评论
    @RequestMapping("/queryCommentByUid")
    @ResponseBody
    public List<Comment> queryCommentByUid(String user_id){
        List<Comment> result=commentService.queryCommentByUid(user_id);
        if (result!=null){
            return result;
        }
        else {
            return null;
        }
    }

    //根据ID查评论
    @RequestMapping("/queryCommentById")
    @ResponseBody
    public Comment queryCommentById(String comment_id){
        Comment comment=commentService.queryCommentById(comment_id);
        if (comment!=null){
            return comment;
        }
        else {
            return null;
        }
    }

    //查看文章下所有评论
    @RequestMapping("/queryListByNews_id")
    @ResponseBody
    public List<Comment>queryListByNews_id(String news_id){
        List<Comment> result=commentService.queryListByNews_id(news_id);
        if (result!=null){
            return result;
        }
        else {
            return null;
        }
    }

    //分页查询
    @RequestMapping("/CommentLimitInformation")
    @ResponseBody
    public CommentInfo queryListLimit(String news_id,String page,String limit){
        CommentInfo commentInfo=commentService.queryListLimit(news_id,page,limit);
        return commentInfo;
    }

    //user修改评论
    @RequestMapping("/updateComment")
    @ResponseBody
    public String updateComment(Comment comment){
        String result=commentService.updateComment(comment);
        if (result==SUCCESS_CODE){
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_NO_COMMENT;
        }
    }

    //删除评论
    @RequestMapping("/deleteComment")
    @ResponseBody
    public String deleteComment(String comment_id){
        String result=commentService.deleteComment(comment_id);
        return result;
    }
}
